package com.wang.treeMap;

import com.wang.hashmap.User;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap<User, String> treeMap = new TreeMap<>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                int n1 = o1.getName().compareTo(o2.getName());
                int n2 = o1.getStuNo()-o2.getStuNo();
                return n1 == 0 ? n2 : n1;
            }
        });
        User u1 = new User("wang1",1);
        User u2 = new User("wang2",2);
        User u3 = new User("wang3",3);
        treeMap.put(u1,"1");
        treeMap.put(u2,"2");
        treeMap.put(u3,"3");
        System.out.println(treeMap.size());
        System.out.println(treeMap.toString());
        //遍历keyset+entrySet
        Set<Map.Entry<User, String>> entries = treeMap.entrySet();
        for (Map.Entry<User, String> entry : entries) {
            System.out.println(entry);
        }
    }
}
